package com.ity.teamanagement.service.Impl;

import com.ity.teamanagement.code.ArticleCode;
import com.ity.teamanagement.entity.Article;
import com.ity.teamanagement.entity.ArticleType;
import com.ity.teamanagement.entity.inputParam.*;
import com.ity.teamanagement.entity.outputResult.ArticleDetailsVo;
import com.ity.teamanagement.entity.outputResult.ArticleListVo;
import com.ity.teamanagement.entity.outputResult.BackTeaListMessage;
import com.ity.teamanagement.exception.ExceptionUtils;
import com.ity.teamanagement.mapper.ArticleMapper;
import com.ity.teamanagement.mapper.ArticleTypeMapper;
import com.ity.teamanagement.result.Result;
import com.ity.teamanagement.service.ArticleService;
import com.ity.teamanagement.utils.PageableDetails;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ity.teamanagement.utils.PageUtils.returnPages;

/**
 * @author coderFu
 * @create 2021/11/10
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleTypeMapper articleTypeMapper;


    @Override
    public Result addArticle(AddArticleParam addArticleParam) {
        Article article = new Article();
        BeanUtils.copyProperties(addArticleParam,article);
        article.setCount(0);
        article.setCreateTime(new Date());
        //存储到数据库
        Integer integer = articleMapper.addArticle(article);
        if (integer <1){
            ExceptionUtils.error(ArticleCode.ARTICLE_ADD_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result deleteArticle(DeleteArticleParam deleteArticleParam) {
        Integer integer = articleMapper.deleteArticle(deleteArticleParam.getId());
        if (integer <1){
            ExceptionUtils.error(ArticleCode.ARTICLE_DELETE_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result updateArticle(UpdateArticleParam updateArticleParam) {
        Article article = new Article();
        BeanUtils.copyProperties(updateArticleParam,article);
        article.setUpdateTime(new Date());
        Integer integer = articleMapper.updateArticle(article);
        if (integer <1){
            ExceptionUtils.error(ArticleCode.ARTICLE_UPDATE_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result getArticleByType(ArticleSelectParam articleSelectParam) {
        List<ArticleListVo> articleListVos =  articleMapper.getArticleByType(articleSelectParam.getType());
        return Result.success(articleListVos);
    }

    @Override
    public PageableDetails getArticleByTypeForPages(Pageable pageable, ArticleSelectParam articleSelectParam) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = articleMapper.getArticleByTypeForTotal(articleSelectParam);
        if (total == 0){
            return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        int pageNum = (pageNumber - 1)*pageSize >=1?(pageNumber - 1)*pageSize:0;
        List<ArticleListVo> articleListMessages = articleMapper.getArticleByTypeForPages(articleSelectParam,pageNum,pageSize);
        return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),articleListMessages);
    }


    @Override
    public Result getArticleDetailsById(SelectArticleParam selectArticleParam) {
        ArticleDetailsVo articleDetailsVo = articleMapper.getArticleDetailsById(selectArticleParam.getId());
        //阅读数目加一
        if (articleDetailsVo != null){
            articleMapper.updateArticleReadCount(selectArticleParam.getId());
        }
        return Result.success(articleDetailsVo);
    }

    @Override
    public Result getArticleType() {
        List<ArticleType> articleTypes = articleTypeMapper.getArticleType();
        return Result.success(articleTypes);
    }
}
